import cv2

# 导入人脸的训练集
cascPath = "haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
faceCascade.load(cascPath)

def capture(path):
    cap = cv2.VideoCapture(0)
    while True:
        # 获得窗口
        ret, frame = cap.read()
        # 显示窗口
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=3,
            minSize=(30, 30),
            flags=cv2.CASCADE_SCALE_IMAGE
        )
        # 检测到人脸就用矩形框选
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        cv2.imshow('I care', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cv2.imwrite(path, frame)
            break
    cap.release()
    cv2.destroyAllWindows()

